/**
 * http://usejsdoc.org/
 */
var req,res,
	fs = require('fs'),
	url = require('url'),
	querystring = require('querystring');

/**创建初始变量函数*/
exports.init = function(response,request){
	res = response;
	req = request;
};

/**创建首页函数*/
exports.index = function(){
	res.render(__dirname+"/calc.jade",{});
};

exports.calculate = function(){
	var pathname = url.parse(req.url).pathname,
		pathStr =  url.parse(req.url).query,
		param = querystring.parse(pathStr);
	
	var type = param.type?parseInt(param.type):0,
		preValue = param.pre?parseFloat(param.pre):0,
		nextValue = param.next?parseFloat(param.next):0,
		ret = 0;
	
	switch (type) {
	case 1: ret = preValue + nextValue;
		break;
	case 2: ret = preValue - nextValue;
		break;
	case 3: ret = preValue * nextValue;
		break;
	case 4: ret = preValue / nextValue;
		break;
	default:
		break;
	}
	ret = '' + ret;
	res.writeHead( 200 , {"Content-Type":"text/html"});
	res.end(ret);	
};